<?php

class SupplierController extends Zend_Controller_Action
{

    private $_supplier = null;

    public function preDispatch()
    {
        $this->_supplier = new Application_Model_Supplier();
    }

    public function init()
    {
        if(!Zend_Auth::getInstance()->hasIdentity())
            $this->_redirect('/auth/');
        
        $this->view->site = $this->getRequest()->getControllerName();
    }

    public function indexAction()
    {
        $supplier = $this->_supplier->getDataJoin();
        $row = $this->_supplier->checkRowSupplier();
        
        $this->view->supplier = $supplier;
        
        if ($row == 0)
          $this->view->notFound = '<div class="notfound">Tidak Ada Data di Halaman Ini</div>';
        else
          $this->view->notFound = '<div class="notfound">Terdapat '.$row.' Data Pada Tabel Ini</div>';
    }

    public function addAction()
    {
        $form = new Application_Form_SupplierForm();
        $this->view->form = $form;
        
        if($this->_request->isPost())
        {
            if($form->isValid($this->_request->getPost()))
            {
                $data = array(
                    'nama_supplier' => $form->getValue('nama_supplier'),
                    'alamat_supplier' => $form->getValue('alamat_supplier'),
                    'provinsi' => $form->getValue('provinsi'),
                    'negara' => $form->getValue('negara'),
                    'kode_pos' => $form->getValue('kode_pos'),
                    'telepon_1' => $form->getValue('telepon_1'),
                    'telepon_2' => $form->getValue('telepon_2'),
                    'fax' => $form->getValue('fax'),
                    'email_supplier' => $form->getValue('email_supplier'),
                    'website_supplier' => $form->getValue('website_supplier'),
                    'keterangan_supplier' => $form->getValue('keterangan_supplier'),
                    'id_status_supplier' => $form->getValue('id_status_supplier'),
                    );
                
                $this->_supplier->supplierBaru($data);
                $this->_redirect('supplier/index');
                } else {
                    $form->populate($this->_request->getPost());
                }
        
        }
    }

    public function editAction()
    {
        $form = new Application_Form_SupplierForm();
        $form->submit->setLabel('Simpan');
        $this->view->form = $form;
        
        if($this->_request->isPost())
        {
            if($form->isValid($this->_request->getPost()))
            {
                $data = array(
                    'nama_supplier' => $form->getValue('nama_supplier'),
                    'alamat_supplier' => $form->getValue('alamat_supplier'),
                    'provinsi' => $form->getValue('provinsi'),
                    'negara' => $form->getValue('negara'),
                    'kode_pos' => $form->getValue('kode_pos'),
                    'telepon_1' => $form->getValue('telepon_1'),
                    'telepon_2' => $form->getValue('telepon_2'),
                    'fax' => $form->getValue('fax'),
                    'email_supplier' => $form->getValue('email_supplier'),
                    'website_supplier' => $form->getValue('website_supplier'),
                    'keterangan_supplier' => $form->getValue('keterangan_supplier'),
                    'id_status_supplier' => $form->getValue('id_status_supplier'),
                );
                
                $this->_supplier->supplierUpdate($data, $form->getValue('id_supplier'));
                $this->_redirect('supplier/index');
                
                }} else {
                    $id = $this->_getParam('id',0);
                    $supplier = $this->_supplier->getSupplier($id);
                    if ($id > 0){
                       $form->populate($supplier);
                    }  
                
        }
    }

    public function deleteAction()
    {
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->getHelper('layout')->disableLayout();
        $id = $this->getRequest()->getParam('id');
        if($id)
        {
            $this->_supplier->supplierDelete($id);
        } else {
            $this->_redirect('supplier');
        }
    }

    public function viewAction()
    {
        $id = $this->_getParam('id');
        if ($id != null) {
            
            $supplier = $this->_supplier->getDataJoinWithId($id);
            
            foreach($supplier as $supplier)
            
            if($supplier == null)
            {
                $this->view->notFound = '<div class="notfound">Data Tidak Ditemukan</div>';
            } else {
                $this->view->supplier = $supplier;
            }
            
        } else {
            $this->_redirect('supplier/index');
        }
    }


}









